package com.kaiupup.study.service;

import com.kaiupup.study.constant.CommonStatusEnum;
import com.kaiupup.study.constant.IdentityConstants;
import com.kaiupup.study.constant.TokenConstants;
import com.kaiupup.study.dto.ResponseResult;
import com.kaiupup.study.remote.ServicePassengerUserClient;
import com.kaiupup.study.remote.ServiceVefificationcodeClient;
import com.kaiupup.study.request.VerificationCodeDTO;
import com.kaiupup.study.response.NumberCodeResponse;
import com.kaiupup.study.response.TokenResponse;
import com.kaiupup.study.util.JwtUtils;
import com.kaiupup.study.util.RedisPrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.concurrent.TimeUnit;

/**
 * @projectName: kaiupup-online-taxi
 * @package: com.kaiupup.study.service
 * @className: VerificationcodeService
 * @author: kaiupup
 * @description: TODO
 * @date: 2024/1/19 18:54
 * @version: 1.0
 */

@Service
@Slf4j
public class VerificationcodeService {

    @Autowired
    private ServiceVefificationcodeClient serviceVefificationcodeClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;
    

    /*
    * 生成验证码
    *
    * */
    public ResponseResult generatorCode(String passengerPhone){
        //远程调用获取验证码服务
        ResponseResult<NumberCodeResponse> numberCodeResponse = serviceVefificationcodeClient.numberCode(6);
        System.out.println("numberCodeResponse = " + numberCodeResponse);
        NumberCodeResponse data = numberCodeResponse.getData();
        int numberCode = data.getNumberCode();

        //存入redis
        String key = RedisPrefixUtils.generatorkeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY);

        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);

        return ResponseResult.success();
    }



    public ResponseResult checkCode(String passengerPhone, String verificationCode) {

        String key = RedisPrefixUtils.generatorkeyByPhone(passengerPhone,IdentityConstants.PASSENGER_IDENTITY);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);

        System.out.println("redis中的value = " + codeRedis);

        if (StringUtils.isBlank(codeRedis)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }

        if (!verificationCode.trim().equals(codeRedis.trim())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }

        //判断原来是否有用户
        VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
        verificationCodeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);

        //颁发令牌,用常量
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        String accessTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);

        String refreshTokenKey = RedisPrefixUtils.generatorTokenKey(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);

        return ResponseResult.success(tokenResponse);
    }


}
